package com.hhqk.controller;



import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName:
 * @version 1.0
 * @Desc: 
 * @author:ghq
 * @date 2017年2月28日下午2:21:11
 * @history v1.0
 */
@Controller
@RequestMapping(value="/api")
public class IndexController {
	
	@RequestMapping(value="/login")   //login.jsp  form表单的提交路径
	public String login(Model mav,String username,String password,HttpSession session){
		System.out.println("login:"+username);
		if(StringUtils.isEmpty(username)||
				StringUtils.isEmpty(password)){
			session.setAttribute("loginFailed", 2);
			return "redirect:/login.jsp";
		}
		
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			subject.getSession().setAttribute("username", username);
//			return "/home/index";
			return "forward:/index";
		} catch (Exception e) {
			System.out.println("login failed:"+username);
			session.setAttribute("loginFailed", 1);
			return "redirect:/login.jsp";
		} 
		
	}
	
}
